from .views import  Page, WebSocket, WebApi
from os import path

def setup_pages(app, front = None, admin = None):

    if front and path.isdir(front):     
        # 主站点
        client = Page(front) 

        app.router.add_get('/', client.index)
        app.router.add_get('/favicon.ico', client.favicon)
        app.router.add_static('/static/', path='%s/static' % front, name='static')

    if admin and path.isdir(admin):
        # 后台管理
        administrator = Page(admin)

        app.router.add_get('/admin', administrator.index)
        # 静态资源
        app.router.add_static('/admin/', path='%s/static' % admin, name='admin')
        print('后台地址：http://127.0.0.1:8087/admin')

def setup_websocket(app, controller = None):
    
    if not controller:
        return

    # 绑定 websocket handler
    ws_app = WebSocket(controller)
    app.router.add_get('/websocket', ws_app.ws_connection)


def setup_api(app, controller = None):

    if not controller:
        return

    api = WebApi(controller)
    app.router.add_get('/time', api.time)
    app.router.add_get('/tasks', api.tasks)
    app.router.add_post('/task/add', api.task_add)
    # app.router.add_post('/task/del', api.task_del)

    app.router.add_get('/aivs', api.aivs)
    # app.router.add_post('/aiv/add', api.aiv_add)
    # app.router.add_post('/aiv/del', api.aiv_del)
    
    app.router.add_post('/where/to/go', api.where_to_go)